package com.moyq5.mvc.maker.test.bean.entity;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;

import com.moyq5.mvc.framework.encrypt.MD5;

/**
 * 告警记录
 * @author Moyq5
 * @date 2018年8月28日
 */
@Entity
@Table(name = "iot_alarm")
@DynamicUpdate
public class IotAlarm implements java.io.Serializable {
	
	private static final long serialVersionUID = 1L;
	/**
	 * 告警ID
	 */
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")@GenericGenerator(name = "native", strategy = "native")
    @Column(name = "alarm_id")
	private Long alarmId;
	/**
	 * 告警设备
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "device_id")
	private IotDevice device;
	/**
     * 告警序号（系统唯一）
     */
    @Column(name = "alarm_no", nullable = false, length = 32)
    private String alarmNo;
    /**
	 * 告警类型
	 */
    @Column(name = "alarm", nullable = false, columnDefinition="int(1)")
	private Integer alarm;
    /**
	 * 告警名称
	 */
	@Column(name = "name", nullable = false, length = 32)
	private String name;
	/**
	 * 告警时间
	 */
	@Column(name = "alarm_time", columnDefinition="datetime")
	private Date alarmTime;
	/**
	 * 告警内容
	 */
	@Column(name = "content", length = 300)
	private String content;
	/**
	 * 添加时间
	 */
	@Column(name = "add_time", nullable = false, columnDefinition="datetime")
	private Date addTime;
	
	@OneToMany(fetch = FetchType.LAZY)
	@JoinColumn(name = "alarm_id")
	private Set<IotGps> gps;
	
	@OneToMany(fetch = FetchType.LAZY)
	@JoinColumn(name = "alarm_id")
	private Set<IotLbs> lbs;
	
	@PrePersist
	public void prePersist() {
		if (null == addTime) {
			addTime = new Date();
		}
		if (null == alarmNo) {
			String key = "" + device.getDeviceId() 
					+ alarmTime + alarm;
			alarmNo = MD5.encrypt2Hex(key);
		}
	}

	public Long getAlarmId() {
		return alarmId;
	}

	public void setAlarmId(Long alarmId) {
		this.alarmId = alarmId;
	}

	public IotDevice getDevice() {
		return device;
	}

	public void setDevice(IotDevice device) {
		this.device = device;
	}

	public String getAlarmNo() {
		return alarmNo;
	}

	public void setAlarmNo(String alarmNo) {
		this.alarmNo = alarmNo;
	}

	public Integer getAlarm() {
		return alarm;
	}

	public void setAlarm(Integer alarm) {
		this.alarm = alarm;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getAlarmTime() {
		return alarmTime;
	}

	public void setAlarmTime(Date alarmTime) {
		this.alarmTime = alarmTime;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getAddTime() {
		return addTime;
	}

	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}

	public Set<IotGps> getGps() {
		return gps;
	}

	public void setGps(Set<IotGps> gps) {
		this.gps = gps;
	}

	public Set<IotLbs> getLbs() {
		return lbs;
	}

	public void setLbs(Set<IotLbs> lbs) {
		this.lbs = lbs;
	}
	
}
